import json
import sys
import time

import stomp

class ActiveMQ:
    def __init__(self, host, port, listener):
        self.conn = stomp.Connection10([(host, port)])

        self.conn.connect()
        self.task_map = {}
        self.listener = listener

    def init_server(self, queue_name):
        self.conn.set_listener(queue_name, self.listener)
        self.conn.subscribe(queue_name)
        while True:
            print('等待任务...')
            time.sleep(10)

    def init_client(self, queue_name):
        self.conn.set_listener(queue_name, self.listener)
        self.conn.subscribe(queue_name)
        while True:
            print('等待任务...')
            time.sleep(2)

    def replay_message(self):
        self.conn.send(body=' '.join(sys.argv[1:]), destination='/queue/test')

    def send_message(self):
        self.conn.send(body=' '.join(sys.argv[1:]), destination='/queue/test')

    def send_to_queue(self, queue_name, msg):
        self.conn.send(queue_name, msg)
        # self.conn.disconnect()
